<?php

namespace app\api\controller;

use app\common\controller\Frontend;
use app\common\model\ProductClassify;
use think\db\exception\ModelNotFoundException;

/**
 * 产品
 */
class Product extends Frontend
{
    protected array $noNeedLogin = ['*'];

    /**
     * 产品列表
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function lst(): void
    {
        $params = $this->request->get(["classify_id", "name"]);
        $where = [];
        if (isset($params['classify_id'])) {
            $where['product_classify_id'] = $params['classify_id'];
        }
        if (isset($params['name'])) {
            $params['name'] = "%" . $params['name'] . "%";
        }

        $productModel = new \app\common\model\Product();
        $res = $productModel
            ->where($where)
            ->whereLike('name', $params['name'] ?? "%%")
            ->order("weigh", "desc")
            ->paginate(20);
        $this->success("产品", $res);

    }

    /**
     * 产品详情
     * @return void
     */
    public function info(): void
    {
        $id = $this->request->get("id");
        $productModel = new \app\common\model\Product();
        $res = $productModel->find($id);
        $this->success("产品详情", $res);
    }


    /**
     * 产品分类
     * @return void
     * @throws ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function classify(): void
    {
        $classModel = new ProductClassify();
        $res = $classModel->order("weigh", "desc")->select();
        $this->success("产品分类", $res);
    }

}